package com.dashu.authority.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.dashu.authority.common.Constant;
import com.dashu.authority.entity.AuthenticationInfo;
import com.dashu.authority.entity.AuthorityOperate;
import com.dashu.authority.entity.ButtonOperate;
import com.dashu.authority.entity.User;
import com.dashu.authority.exception.AuthorityException;
import com.dashu.authority.servie.AuthenticationInfoService;
import com.dashu.authority.servie.AuthorityOperateService;
import com.dashu.authority.servie.ButtonOperateService;
import com.dashu.authority.servie.UserService;
import com.dashu.authority.util.AuthorityServiceUtil;
import com.dashu.authority.util.Competence;
import com.dashu.authority.util.ResultBean;
import com.dashu.authority.util.ResultMsgUtil;
import com.github.pagehelper.PageInfo;

@Controller
@RequestMapping("/buttonOperate")
public class ButtonOperateController extends BaseController {

	@Resource
	private AuthenticationInfoService infoService;
	@Resource
	private UserService userService;
	@Resource
	private ButtonOperateService buttonService;
	@Resource
	private AuthorityOperateService authorityService;

	@RequestMapping("/findAllByPaging")
	public @ResponseBody Object findAllByPaging(ButtonOperate entity, Integer page, Integer rows)
			throws AuthorityException {
		List<ButtonOperate> returnList = buttonService.queryAllByExample(entity, page, rows);
		Map<String, Object> returnMap = ResultMsgUtil.datagridFormat(new PageInfo<ButtonOperate>(returnList));
		return new ResultBean(Constant.STATUS_NORMAL, returnMap).getResultMap();
	}

	@RequestMapping(value = "/save")
	public @ResponseBody Object addMenu(ButtonOperate entity) throws AuthorityException {
		Map<String, Object> returnMap = buttonService.insertEntity(entity);
		return new ResultBean(returnMap).getResultMap();
	}

	@RequestMapping(value = "/update")
	public @ResponseBody Object updateMenu(ButtonOperate entity) throws AuthorityException {
		Map<String, Object> returnMap = buttonService.updateEntity(entity);
		return new ResultBean(returnMap).getResultMap();
	}

	@RequestMapping(value = "/deleteByIds")
	public @ResponseBody Object deleteByIds(@RequestParam(value = "ids") Integer[] ids) throws AuthorityException {
		Map<String, Object> returnMap = buttonService.deleteByIds(ids);
		return new ResultBean(returnMap).getResultMap();
	}

	// 回去菜单按钮 根据用户权限
	@RequestMapping("/findAllByPagingAndAuth")
	public @ResponseBody Object findAllByPagingAndAuth(ButtonOperate entity,String DASHU_AUTH_TOKEN) throws AuthorityException {
		AuthorityServiceUtil.checkParam("菜单id", entity);
		AuthorityServiceUtil.checkParam("菜单id", entity.getMenuId());
		AuthenticationInfo info = this.infoService.queryInfoByToken(DASHU_AUTH_TOKEN);// 查看token
		if (info == null) {
			throw new AuthorityException("2100002");
		}
		User user = userService.findAccountWithUserName(info.getLoginId());
		if (user == null) {
			throw new AuthorityException("2100002");
		}
		List<ButtonOperate> returnList = buttonService.queryAllByExample(entity,1,1000);
		if (!user.getIsSuperAdmin()&&returnList!=null&&returnList.size()>0) {
			Map<Integer, Long> auths = this.authorityService.getAuthorityByUserAndType(user.getId(),
					Constant.RESOURCES_TYPE_MENU, null, entity.getMenuId());
			if (auths == null || auths.get(entity.getMenuId()) == null) {
				returnList = new ArrayList<ButtonOperate>();
			} else {
				Long l = auths.get(entity.getMenuId());
				List<ButtonOperate> list = new ArrayList<ButtonOperate>();
				Competence c = new Competence();
				for (int i = 0; i < returnList.size(); i++) {
					if (c.checkProduct(l, returnList.get(i).getSoreNum())) {
						list.add(returnList.get(i));
					}
				}
				returnList=list;				
			}
		}
		Map<String, Object> returnMap = ResultMsgUtil.datagridFormat(new PageInfo<ButtonOperate>(returnList));
		return new ResultBean(Constant.STATUS_NORMAL, returnMap).getResultMap();
	}

}
